/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.ClieUserDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.model.ClieUser;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class ClieUserDaoImpl extends BaseDaoImpl implements ClieUserDao {
	public List getList(boolean justEnabled, String searchKey) {
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		String sql = "select u.id as u_id,u.user_name,s.est_name,u.enabled,u.pric_type,u.orde_type,s.tally_set_id from clie_user u left join establishment s on s.id=u.clie_id where 1=1";
		if (justEnabled) {
			sql = sql + " and u.enabled=\'Y\'";
		}

		if (searchKey != null && !searchKey.equals("")) {
			sql = sql
					+ " and ((u.user_name like :searchKey) or (s.est_name like :searchKey) or (s.name_spell like :searchKey))";
		}

		sql = sql + " order by u.enabled desc,u.user_name";
		Session sess = null;

		List arg6;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("searchKey", "%" + searchKey + "%");
			}

			arg6 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg6;
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.ClieUser where id = :id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int unenabledClieUser(final long clieId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.ClieUser set enabled=\'N\' where clieId = :clieId";
						return new Integer(session.createQuery(hql)
								.setLong("clieId", clieId).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public ClieUser get(String userName) {
		String[] paramNames = new String[]{"userName"};
		Object[] values = new Object[]{userName};
		List list = this.getHibernateTemplate().findByNamedParam(
				"from com.minierp.model.ClieUser u where u.userName=:userName",
				paramNames, values);
		return !list.isEmpty() ? (ClieUser) list.get(0) : null;
	}
}